from typing import Union

from gradio.utils import NamedString
from langchain_core.embeddings import Embeddings

import knowledge_base_da
import shutil
import uuid
import os
import config


def create_knowledge_base(knowledge_base_name: str):
    return knowledge_base_da.create_knowledge_base_data(knowledge_base_name)


def create_knowledge_base_data(knowledge_base_name: str, filepath: Union[list[NamedString], str], embedding: Embeddings):
    directory=""
    if not isinstance(filepath, str):  # 传入一个文件路径列表
        random_path = uuid.uuid4().hex[-32:]
        directory = f"{config.upload_document_path}\\{random_path}"
        os.makedirs(directory)
        for file in filepath:
            shutil.move(file, directory)
    return knowledge_base_da.create_knowledge_base_data(knowledge_base_name, directory, embedding)


def get_knowledge() -> list:
    return knowledge_base_da.get_knowledge()


def get_knowledge_list() -> list[list[str]]:
    return knowledge_base_da.get_knowledge_list()


def get_knowledge_base_file(collection_name: str, embedding: Embeddings):
    return knowledge_base_da.get_knowledge_base_file(collection_name, embedding)
